//
//  OrderTitleCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class OrderTitleCell: UITableViewCell {
    static var identifier = "OrderTitleCell.swordjoy"

    static func heightWithTitle(title: String) -> CGFloat {
        return UIFont.calculateHeight(width: UIScreen.width - 40, font: GlobalConfig.bold20Font, text: title) + 68
    }

    // 工单详情
    var model: OrderDetailModel? {
        didSet {
            guard let temp = model else { return }

            stateLabel.text = self.getState(level: temp.priorityLevel)
            stateLabel.textColor = self.getSateColor(level: temp.priorityLevel)

            typeLabel.text = temp.category
            codeLabel.text = temp.number
            titleLabel.text = temp.title

//            if "FINISHED" == temp.status {
//                resultV.isHidden = false
//                resultImageV.isHidden = false
//                resultLabel.text = "完结"
//            }
        }
    }
  
  fileprivate var stateLabel: UILabel!
  fileprivate var typeLabel: UILabel!
  fileprivate var codeLabel: UILabel!
  fileprivate var titleLabel: UILabel!
  
  fileprivate var resultV: UIView!
  fileprivate var resultLabel: UILabel!
  fileprivate var resultImageV: UIImageView!
    
    // 已检状态,执行的人
    fileprivate var executorPeople: UILabel!
    
    fileprivate var cooperateImageView: UIImageView!
    fileprivate var cooperateStaff: UILabel!
    fileprivate var cooperateContainerView: UIView!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor.white
    
    selectionStyle = .none
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension OrderTitleCell {
  
  fileprivate func getState(level: String) -> String {
    
    if "NORMAL" == level { return "一般"}
    else if "URGENT" == level { return "紧急"}
    else if "VERY_URGENT" == level { return "非常紧急" }
    
    return ""
    
  }
  
  fileprivate func getSateColor(level: String) -> UIColor {
  
    if "NORMAL" == level { return GlobalConfig.PaleTealColor }
    
    return GlobalConfig.GrapeFruitColor
    
  }
  
  fileprivate func create() {
    stateLabel = UILabel()
    contentView.addSubview(stateLabel)
    stateLabel.font = GlobalConfig.system12Font
    stateLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(10)
    }
    
    typeLabel = UILabel()
    contentView.addSubview(typeLabel)
    typeLabel.font = GlobalConfig.system12Font
    typeLabel.textColor = GlobalConfig.TextLightColor
    typeLabel.snp.makeConstraints { (make) in
      make.left.equalTo(stateLabel.snp.right).offset(20)
      make.top.equalTo(10)
    }
    
    codeLabel = UILabel()
    contentView.addSubview(codeLabel)
    codeLabel.font = GlobalConfig.system12Font
    codeLabel.textColor = GlobalConfig.TextLightColor
    codeLabel.snp.makeConstraints { (make) in
      make.right.equalTo(-20)
      make.top.equalTo(10)
    }
    
    titleLabel = UILabel()
    contentView.addSubview(titleLabel)
    titleLabel.numberOfLines = 0
    titleLabel.font = GlobalConfig.bold20Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(stateLabel.snp.bottom).offset(10)
      make.width.equalTo(UIScreen.width-40)
    }
    
    executorPeople = UILabel()
    contentView.addSubview(executorPeople)
    executorPeople.textColor = GlobalConfig.BGSkyBlueColor
    executorPeople.textAlignment = .right
    executorPeople.font = GlobalConfig.system14Font
    executorPeople.snp.makeConstraints { (make) in
        make.right.equalTo(-20)
        make.top.equalTo(42)
        make.width.equalTo(80)
    }
    
    resultV = UIView()
    contentView.addSubview(resultV)
    resultV.backgroundColor = UIColor.clear
    resultV.snp.makeConstraints { (make) in
      make.height.equalTo(30)
      make.top.equalTo(executorPeople.snp.bottom).offset(2)
      make.right.equalTo(-20)
      make.width.equalTo(60)
    }
    resultV.isHidden = true
    
    
    
    resultImageV = UIImageView()
    resultV.addSubview(resultImageV)
    resultImageV.image = UIImage(named: "over-icon")
    resultImageV.snp.makeConstraints { (make) in
      make.left.centerY.equalToSuperview()
      make.height.equalTo(10)
      make.width.equalTo(14)
    }
    
    resultLabel = UILabel()
    resultV.addSubview(resultLabel)
    resultLabel.textColor = GlobalConfig.BGSkyBlueColor
    resultLabel.font = GlobalConfig.system16Font
    resultLabel.snp.makeConstraints { (make) in
      make.centerY.equalToSuperview()
      make.right.equalToSuperview()
    }

    cooperateContainerView = UIView()
    cooperateContainerView.backgroundColor = UIColor.white
    contentView.addSubview(cooperateContainerView)
    cooperateContainerView.snp.makeConstraints { (make) in
        make.left.bottom.equalToSuperview()
        make.top.equalTo(titleLabel.snp.bottom)
        make.right.equalTo(-80)
    }
    cooperateContainerView.isHidden = true
    
    cooperateImageView = UIImageView()
    cooperateContainerView.addSubview(cooperateImageView)
    cooperateImageView.image = UIImage(named: "cooperateInspection")
    cooperateImageView.snp.makeConstraints { (make) in
        make.left.equalTo(20)
        make.top.equalTo(10)
        make.height.width.equalTo(14)
    }
    
    cooperateStaff = UILabel()
    cooperateContainerView.addSubview(cooperateStaff)
    cooperateStaff.font = GlobalConfig.system14Font
    cooperateStaff.textColor = GlobalConfig.TextLightColor
    cooperateStaff.numberOfLines = 0
    cooperateStaff.snp.makeConstraints { (make) in
        make.left.equalTo(40)
        make.right.equalToSuperview()
        make.top.equalTo(10)
    }
    
  }
}
